package com.xpalive;

import com.xpalive.config.Property;
import com.xpalive.helper.DocReader;
import com.xpalive.helper.PropertiesReader;
import com.xpalive.pojo.DocAttr;
import com.xpalive.pojo.UrlAttr;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

/**
 * Created by xiongping on 2017/5/21.
 */
public class App {
    private final static Logger LOGGER = LoggerFactory.getLogger(App.class);

    static {
        PropertiesReader.load();
    }

    public static void main(String[] args) {
        DocReader docReader = new DocReader();
        LOGGER.info("start:{}",new Date());
        recursion(docReader,"","",Property.getRoot(),0);
        LOGGER.info("end:{}",new Date());
    }

    private static void recursion(DocReader docReader, String fromUrlText,String fromUrl, String url, int level){
        ++level;
        if (level < Property.getLevel()) {
            DocAttr docAttr = docReader.getByUrl(fromUrlText,fromUrl, url);
            if (null != docAttr && !docAttr.isNotFoundPage() && !docAttr.isNotFoundPage()) {
                List<UrlAttr> docUrls = docAttr.getDocUrls();
                if (null != docUrls){
                    for (UrlAttr docUrl : docUrls) {
                        recursion(docReader, docUrl.getHtml(),docUrl.getFrom(), docUrl.getUrl(), level);
                    }
                }
            }
        }
    }
}
